$(document).ready(function() {
	
	Highcharts.setOptions({
		global: {
			useUTC: false
		}
	});
	
	var tempData = new dataSet(0,0,0,0,0,0);
	
	function dataSet(cpu,usedMemory,freeMemory,diskWrite,diskRead,date)
	{
		dataSet.cpu=cpu/100
		dataSet.usedMemory=usedMemory
		dataSet.freeMemory=freeMemory
		dataSet.diskWrite=diskWrite
		dataSet.diskRead=diskRead
		dataSet.date=date
	}
	
	function setData()
	{
		$.getJSON("http://10.1.7.113:8080/Distributed_Server_Performance_Monitor/services/performancedata/1.1.1.1/last",function(result){
		    var jsonobj=eval(result);
		    tempData.cpu=Math.round(jsonobj.cpu*100)/100.0 ;
		    tempData.date=jsonobj.createDate;
		    tempData.diskRead = jsonobj.diskRead;
		    tempData.diskWrite=jsonobj.diskWrite;
		    tempData.usedMemory=jsonobj.usedMenory;
		    tempData.freeMemory=jsonobj.freeMenory;
		  });
	}
	
	setInterval(setData,500);
	
	var chart1;
	// Create the chart
    chart1 = new Highcharts.StockChart({
        chart : {
            renderTo : 'apDiv2',
            events : {
                load : function() {

                    // set up the updating of the chart each second
                    var series = this.series[0];
                    setInterval(function() {
                    	var x = (new Date()).getTime();
					    var y = tempData.cpu;
					    series.addPoint([x, y], true, true);
                    }, 1000);
                }
            }
        },
        

        
        rangeSelector: {
            buttons: [
                {
            	count: 10,
                type: 'second',
                text: '1S'
                	
                },{
                count: 1,
                type: 'minute',
                text: '1M'
            }, {
                count: 5,
                type: 'minute',
                text: '5M'
            }, {
                type: 'all',
                text: 'All'
            }],
            inputEnabled: false,
            selected: 0
        },
        
        title : {
            text : 'CPU data'
        },
        
        exporting: {
            enabled: false
        },
        
        yAxis : {
			title : {
				text : 'CPU rate'
			},
			max:100.0,
			min:0.0,
		},
        
        series : [{
            name : 'Random data',
            data : (function() {
                // generate an array of random data
                var data = [], time = (new Date()).getTime(), i;

                for( i = -3600; i <= 0; i++) {
					data.push([
						time + i * 1000,
						Math.floor(Math.random()*100) 
					]);
				}
                return data;
            })()
        }]
    });
	
	var chart2;
	chart2 = new Highcharts.Chart({
		chart: {
			renderTo: 'apDiv3',
			type: 'spline',
			marginRight: 10,
			events: {
				load: function() {
					// set up the updating of the chart each second
					var series = this.series[0];
					setInterval(function() {
						//$.getJSON("http://10.1.7.235:8080/testjson/services/performancedata/1.1.1.1/last",function(result){
						//    var jsonobj=eval(result);
						    var x = (new Date()).getTime();
						    var y = tempData.usedMemory;
						    series.addPoint([x, y], true, true);
						//  });
					}, 1000);
				}
			}
		},
		title: {
			text: 'UsedMemory data'
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 150
		},
		yAxis: {
			title: {
				text: 'Value'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#808080'
			}]
		},
		tooltip: {
			formatter: function() {
					return '<b>'+ this.series.name +'</b><br/>'+
					Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
					Highcharts.numberFormat(this.y, 2);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [{
			name: 'Random data',
			data: (function() {
				// generate an array of random data
				var data = [],
					time = (new Date()).getTime(),
					i;

				for (i = -10; i <= 0; i++) {
					data.push({
						x: time + i * 1000,
						y: 0
					});
				}
				return data;
			})()
		}]
	});
	
	var chart3;
	chart3 = new Highcharts.Chart({
		chart: {
			renderTo: 'apDiv4',
			type: 'spline',
			marginRight: 10,
			events: {
				load: function() {
					
				
					// set up the updating of the chart each second
					var series = this.series[0];
					setInterval(function() {
						//$.getJSON("http://10.1.7.235:8080/testjson/services/performancedata/1.1.1.1/last",function(result){
						    //var jsonobj=eval(result);
						    var x = (new Date()).getTime();
						    var y = tempData.diskRead;
						    series.addPoint([x, y], true, true);
						//  });
					}, 1000);
				}
			}
		},
		title: {
			text: 'diskRead data'
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 150
		},
		yAxis: {
			title: {
				text: 'Value'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#808080'
			}]
		},
		tooltip: {
			formatter: function() {
					return '<b>'+ this.series.name +'</b><br/>'+
					Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
					Highcharts.numberFormat(this.y, 2);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [{
			name: 'Random data',
			data: (function() {
				// generate an array of random data
				var data = [],
					time = (new Date()).getTime(),
					i;

				for (i = -10; i <= 0; i++) {
					data.push({
						x: time + i * 1000,
						y: Math.random()
					});
				}
				return data;
			})()
		}]
	});
	var chart4;
	chart4 = new Highcharts.Chart({
		chart: {
			renderTo: 'apDiv5',
			type: 'spline',
			marginRight: 10,
			events: {
				load: function() {
					
				
					// set up the updating of the chart each second
					var series = this.series[0];
					setInterval(function() {
						//$.getJSON("http://10.1.7.235:8080/testjson/services/performancedata/1.1.1.1/last",function(result){
						   // var jsonobj=eval(result);
						    var x = (new Date()).getTime();
						    var y = tempData.diskWrite;
						    series.addPoint([x, y], true, true);
						//  });
					}, 1000);
				}
			}
		},
		title: {
			text: 'diskWrite data'
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 150
		},
		yAxis: {
			title: {
				text: 'Value'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#808080'
			}]
		},
		tooltip: {
			formatter: function() {
					return '<b>'+ this.series.name +'</b><br/>'+
					Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
					Highcharts.numberFormat(this.y, 2);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [{
			name: 'Random data',
			data: (function() {
				// generate an array of random data
				var data = [],
					time = (new Date()).getTime(),
					i;

				for (i = -10; i <= 0; i++) {
					data.push({
						x: time + i * 1000,
						y: Math.random()
					});
				}
				return data;
			})()
		}]
	});
});
